package cn.phonevane.action;



import java.util.Date;
import java.util.List;

import cn.phonevane.base.BaseAction;
import cn.phonevane.model.TUser;
import cn.phonevane.service.UserService;
import cn.phonevane.utils.SystemConstant;

public class UserAction extends BaseAction{
	
	
	
	private UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/**
	 * 用户注册�û
	 */
	public String register(){
		String username=httpServletRequest.getParameter("username");
		String password1=httpServletRequest.getParameter("password1");
		String password2=httpServletRequest.getParameter("password2");
		if(username==null||username.equals("")){
			String str="帐号不能为空!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else if(password1==null||password1.equals("")){
			String str="密码不能为空!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
		}else if(password2==null||password2.equals("")){
			String str="密码不能为空!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else if(!password1.equals(password2)){
			String str="您两次输入的密码不相等!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else if(username.length()<4){
			String str="帐号最少6位!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
		}else if(username.length()>21){
			String str="帐号不宜太长!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
		}
		else if(password1.length()<6){
			String str="密码至少为6位!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
		}else if(password1.length()>49){
			String str="密码不宜太长!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else if(this.userService.checkUsername(username)){
				String str="该用户已经存在!";
				httpServletRequest.setAttribute("WARN", str);
				return ERROR;
		}else{
			TUser tUser=new TUser();
			String firstIp=httpServletRequest.getRemoteAddr();
			tUser.setUserName(username);
			tUser.setUserPass(password1);
			tUser.setUserIfVip(SystemConstant.IF_VIP);
			tUser.setUserVipLevel(SystemConstant.VIP_LEVEL);
			tUser.setUserIfAuthor(SystemConstant.IF_AUTHOR);
			tUser.setUserAuthorLevel(SystemConstant.AUTHOR_LEVEL);
			tUser.setUserIfAdmin(SystemConstant.IF_ADMIN);
			tUser.setUserAdminLimit(SystemConstant.ADMIN_LIMIT);
			tUser.setUserLocationIp(firstIp);
			tUser.setUserCreateTime(new Date());
			tUser.setUserHeadName(SystemConstant.HEADER_NAME);
			tUser.setUserHeadUrl(SystemConstant.HEADER_IMAGES_URL);
			boolean sign=this.userService.add(tUser);
			if(sign==true){
				return SUCCESS;		
			}	
		}
		return ERROR;
	}
	
	
	
	/**
	 * 用户登录�û
	 */
	public String login(){
		TUser tUser=null;
		String username=httpServletRequest.getParameter("username");
		String password=httpServletRequest.getParameter("password");
		if(username==null||username.equals("")){
			String str="帐号不能为空!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else if(password==null||password.equals("")){
			String str="密码不能为空!";
			httpServletRequest.setAttribute("WARN", str);
			return ERROR;
			
		}else{
			tUser=this.userService.check(username,password);
			if(tUser!=null){
				httpServletRequest.getSession().setAttribute("USER", tUser);
				httpServletRequest.getSession().setMaxInactiveInterval(1800);
				return SUCCESS;
			}else{
				String str="帐号或密码错误!";
				httpServletRequest.setAttribute("WARN",str );
			}
			
		}
		return ERROR;
	}

	
	
}
